package com.aqie.rabbit.api.limit;

import com.rabbitmq.client.*;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

@Slf4j
public class ProducerTest {
    private static final String IP_ADDRESS = "127.0.0.1";
    private static final String VIRTUAL_HOST = "/";
    private static final String EXCHANGE_NAME = "exchange_qos_limit";
    private static final String ROUTING_KEY = "limit.#";
    private static final int PORT = 5672;

    public static void main(String[] args) throws IOException, TimeoutException {
        //1 创建一个ConnectionFactory, 并进行配置
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(IP_ADDRESS);
        connectionFactory.setPort(PORT);
        connectionFactory.setVirtualHost(VIRTUAL_HOST);

        //2 通过连接工厂创建连接
        Connection connection = connectionFactory.newConnection();

        //3 通过connection创建一个Channel
        Channel channel = connection.createChannel();

        //4 通过Channel发送数据
        String msg = "Hello RabbitMQ! limit message";


        for (int i = 0; i < 5; i++) {
            channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY, true, null, msg.getBytes());
        }
        // 7. 记得要关闭相关的连接
        /*channel.close();
        connection.close();*/
    }
}
